{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Qiskit basics\n",
"\n",
"Qiskit is an open-source framework for working with quantum computers. It is maintained and developed by IBM. It provides tools for creating, manipulating and simulating quantum circuits. Qiskit also includes libraries for different quantum algorithms. It has a developer community (available on Slack) and detailed documentation (Qiskit documentation).
\n",
"\n",
"The foundation of the Qiskit is module Terra, which provides the essential tools to build and run quantum circuits. In addition, Terra defines the interfaces for an end-user experience and the layers of optimization, pulse scheduling, and backend communication. The detailed and more comprehensive tutorial can be found here.\n",
"\n",
"\n",
"\n",
"Aer module has different emulators that can imitate the working quantum computer. Aer provides a high-performance emulator framework for quantum circuits using the Qiskit software stack. It contains optimized C++ emulator backends for executing circuits compiled in Terra.\n",
"\n",
"\n",
"To create a quantum circuit, we need to create an instance of the `QuantumCircuit` class and specify the number of qubits.\n"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-10-07T13:15:58.026026Z",
"start_time": "2024-10-07T13:15:57.672165Z"
}
},
"outputs": [
{
"data": {
"text/plain": " \nq_0: \n \nq_1: \n \nq_2: \n \nq_3: \n ",
"text/html": "
\nq_0: \n \nq_1: \n \nq_2: \n \nq_3: \n" }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from qiskit import QuantumCircuit\n", "\n", "circuit = QuantumCircuit(4) # Create a QuantumCircuit object with 4 qubits\n", "circuit.draw() # Visualize the circuit (now, it is an empty circuit, but one should notice that the designation of the qubits starts from zero)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "After creating the empty circuit with its quantum registers, we can add the operators (gates) to develop and execute the circuit. In Qiskit, operators can be added to the circuit one by one. The complete list of available gates and their usage can be found in Qiskit's documentation.\n", "\n", "For instance, see: qiskit gates.\n", "\n", "It is important to note that the gates (operators) are added to the circuit as a method call on the same circuit (QuantumCircuit) object." ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false, "ExecuteTime": { "end_time": "2024-10-07T13:16:00.025410Z", "start_time": "2024-10-07T13:16:00.019881Z" } }, "outputs": [ { "data": { "text/plain": " ┌───┐ ░ ┌─┐ \n q_0: ┤ X ├──────░─┤M├─────────\n └───┘ ░ └╥┘┌─┐ \n q_1: ───────────░──╫─┤M├──────\n ┌───┐ ░ ║ └╥┘┌─┐ \n q_2: ┤ H ├──■───░──╫──╫─┤M├───\n └───┘┌─┴─┐ ░ ║ ║ └╥┘┌─┐\n q_3: ─────┤ X ├─░──╫──╫──╫─┤M├\n └───┘ ░ ║ ║ ║ └╥┘\nmeas: 4/══════════════╩══╩══╩══╩═\n 0 1 2 3 ", "text/html": "
┌───┐ ░ ┌─┐ \n q_0: ┤ X ├──────░─┤M├─────────\n └───┘ ░ └╥┘┌─┐ \n q_1: ───────────░──╫─┤M├──────\n ┌───┐ ░ ║ └╥┘┌─┐ \n q_2: ┤ H ├──■───░──╫──╫─┤M├───\n └───┘┌─┴─┐ ░ ║ ║ └╥┘┌─┐\n q_3: ─────┤ X ├─░──╫──╫──╫─┤M├\n └───┘ ░ ║ ║ ║ └╥┘\nmeas: 4/══════════════╩══╩══╩══╩═\n 0 1 2 3" }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "circuit.h(2) # Add a H (Hadamard) gate on qubit 3\n", "circuit.cx(2, 3) # Add a CX (CNOT) gate on control qubit 3, and target qubit 4\n", "circuit.x(0) # Add Pauli X gate on the first qubit\n", "circuit.measure_all()\n", "circuit.draw() # Draw the circuit" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false, "ExecuteTime": { "end_time": "2024-10-07T13:16:01.864093Z", "start_time": "2024-10-07T13:16:01.302360Z" } }, "outputs": [ { "data": { "text/plain": "